//点击返回顶部
let scrolltop = document.documentElement.scrollTop
$("#foot ul .li3").click(function () {
    $("html,body").animate({ scrollTop: 0 }, 500)
})
//点击返回图标返回首页
$("#header .fh").click(function(){
    window.location.href="../index.html"
})
//渲染商铺
fetch("http://chst.vip:1234/api/getgsshop")
.then(body=>body.json())
.then(res=>{
    let arr = res.result
    let str = ""
    arr.forEach(item => {
        str+=`
        <li shopId=${item.shopId}>${item.shopName}</li>
        `
    });
    $(".data1 ul").html(str)
})
//渲染地区
fetch("http://chst.vip:1234/api/getgsshoparea")
.then(body=>body.json())
.then(res=>{
    let arr = res.result
    let str = ""
    arr.forEach(item => {
        str+=`
        <li areaId=${item.areaId}>${item.areaName}</li>
        `
    });
    $(".data2 ul").html(str)
})
//初始化默认渲染页面结构
function render(shopid=0,areaid=0){
    fetch(`http://chst.vip:1234/api/getgsproduct?shopid=${shopid}&areaid=${areaid}`)
    .then(body=>body.json())
    .then(res=>{
        let arr = res.result
        let str = ""
        arr.forEach(item => {
            str+=`
            <div class="boxpb">
                    <img src=${item.productImg} alt="">
                    <p class="jieshao">
                        ${item.productName}
                    </p>
                    <p class="jiage">
                        ${item.productPrice}
                    </p>
                </div>
            `
        });
        $("#main .lis li").html(str)
    })
}
render()
//点击事件前声明需要根据点击改变的两个query传参全局变量
var shopid=0
var areaid=0
$(".data1 ul").on("click","li",function(e){
    let _this = e.target
    let text = $(_this).text()
    $("#nav .top .top_ul li:eq(0)").text(`${text}▼`)
    shopid = parseInt($(_this).attr("shopId"))
    render(shopid,areaid)
})
$(".data2 ul").on("click","li",function(e){
    let _this = e.target
    let text = $(_this).text()
    //截取处理text-------------------------------
    let reg =  /^[\d\D]*?(?=\(|（)/
    text = reg.exec(text)[0]
    //-------------------------------------------
    $("#nav .top .top_ul li:eq(1)").text(`${text}▼`)
    areaid = parseInt($(_this).attr("areaid"))
    render(shopid,areaid)
})
//点击nav显示区改变内容区block
var flag1 = false
var flag2 = false
$("#nav .top .top_ul li:eq(0)").click(function(){
    if(flag2){
        $(".data2").toggleClass("hide")
        flag2 = !flag2
    }
    $(".data1").toggleClass("hide")
    flag1 = !flag1
})
$("#nav .top .top_ul li:eq(1)").click(function(){
    if(flag1){
        $(".data1").toggleClass("hide")
        flag1 = !flag1
    }
    $(".data2").toggleClass("hide")
    flag2 = !flag2
})